﻿using Acme.BookStore.RoomTypes;
using Microsoft.AspNetCore.Mvc;
using SC.UserCenter.Infra;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Volo.Abp.Application.Services;
using Volo.Abp.Domain.Repositories;

namespace Acme.BookStore.RoomNos
{
    [ApiExplorerSettings(GroupName = "v2")]
    public class RoomNoServices : ApplicationService, IRoomNoServices
    {
        IRepository<RoomNumber> repository;
        IRepository<RoomType> roomTypeRepository;

        public RoomNoServices(IRepository<RoomNumber> repository, IRepository<RoomType> roomTypeRepository)
        {
            this.repository = repository;
            this.roomTypeRepository = roomTypeRepository;
        }
        /// <summary>
        /// 删除房号
        /// </summary>
        /// <param name="id">Id</param>
        /// <returns>返回任务</returns>
        [HttpDelete("DeleteRoomNo")]
        public async Task<ApiResult> DeleteRoomNo(Guid id)
        {
            try
            {
                var roomNo = await repository.GetAsync(x => x.Id == id);
                if (roomNo == null)
                {
                    return ApiResult.Fail("房号不存在");
                }

                await repository.DeleteAsync(roomNo);
                return ApiResult.Ok();
            }
            catch (Exception ex)
            {
                return ApiResult.Fail(msg: ex.Message);
            }
        }

        /// <summary>
        /// 添加房号
        /// </summary>
        /// <param name="roomNoDTO">实体</param>
        /// <returns>返回方式</returns>
        [HttpPost("RoomNoAdd")]
        public async Task<ApiResult> RoomNoAdd(RoomNoDTO roomNoDTO)
        {
            try
            {
                var list = ObjectMapper.Map<RoomNoDTO, RoomNumber>(roomNoDTO);
                await repository.InsertAsync(list);
                return ApiResult.Ok(data: list);
            }
            catch (Exception ex)
            {
                ex.HelpLink = "操作失败";
                return ApiResult.Fail(msg: ex.Message);
            }
        }

        /// <summary>
        /// 房号显示
        /// </summary>
        /// <param name="roomTypeId">房型iD</param>
        /// <param name="roomNO">编号</param>
        /// <param name="roomStatus">状态</param>
        /// <returns>返回任务</returns>
        /// <exception cref="NotImplementedException">实体</exception>
        [HttpGet("RoomNoShow")]
        public async Task<ResultDto<List<RoomNoTypeDTO>>> RoomNoShow(Guid? roomTypeId, string? roomNO, bool? roomStatus)
        {
            try
            {
                var roomTypes = await roomTypeRepository.GetListAsync();
                var roomNos = await repository.GetListAsync();

                var list = from a in roomTypes
                           join b in roomNos on a.Id equals b.RoomTypeId
                           where b.IsDeleted == false
                           select new RoomNoTypeDTO
                           {
                                Id=b.Id,
                               RoomNO = b.RoomNO,
                               RoomTypeId = b.RoomTypeId,
                               RoomName = a.RoomName,
                               RoomSort = b.RoomSort,
                               RoomStatus = b.RoomStatus,
                               RoomRemark = b.RoomRemark
                           };
                if (roomTypeId != null)
                {
                    list = list.Where(a => a.RoomTypeId == roomTypeId.Value);
                }
                if (!string.IsNullOrEmpty(roomNO))
                {
                    list = list.Where(a => a.RoomNO == roomNO);
                }
                if (roomStatus == true)
                {
                    list = list.Where(a => a.RoomStatus == true);
                }
                if (roomStatus == false)
                {
                    list = list.Where(a => a.RoomStatus == false);
                }

                return new ResultDto<List<RoomNoTypeDTO>> { Data = list.ToList(), Code = StatusCode.Success };
            }
            catch (Exception ex)
            {
                return new ResultDto<List<RoomNoTypeDTO>> { Data = null, Code = StatusCode.Faield, Message = ex.Message };
            }
        }
        /// <summary>
        /// 批量修改状态
        /// </summary>
        /// <param name="ids">集合</param>
        /// <returns>返回实体</returns>
        [HttpPut("UpdateState")]
        public async Task<ApiResult> UpdateAllState(List<Guid> ids)
        {
            try
            {
                var list = await repository.GetListAsync(x => ids.Contains(x.Id));
                foreach (var item in list)
                {
                    item.RoomStatus = !item.RoomStatus;
                }
                await repository.UpdateManyAsync(list);
                return ApiResult.Ok();
            }
            catch (Exception ex)
            {
                return ApiResult.Fail(msg: ex.Message);
            }
        }

        /// <summary>
        /// 修改房号
        /// </summary>
        /// <param name="RoomupdateDTO">实体</param>
        /// <returns>返回任务</returns>

        [HttpPut("UpdateRoomNo")]
        public async Task<ApiResult> UpdateRoomNo(RoomNoUpdateDTO RoomupdateDTO)
        {
            try
            {
                var roomNo = await repository.FirstOrDefaultAsync(x => x.Id == RoomupdateDTO.Id);
                if (roomNo == null)
                {
                    return ApiResult.Fail("房号不存在");
                }

             ObjectMapper.Map(RoomupdateDTO, roomNo);

                await repository.UpdateAsync(roomNo);

                return ApiResult.Ok(data: roomNo);
            }
            catch (Exception ex)
            {
                return ApiResult.Fail(msg: ex.Message);
            }
        }

        /// <summary>
        /// 修改房号状态
        /// </summary>
        /// <param name="id">Id</param>
        /// <param name="newStatus">状态</param>
        /// <returns>返回任务</returns>
        [HttpPut("UpdateRoomStatus")]
        public async Task<ApiResult> UpdateRoomStatus(Guid id)
        {
            try
            {
                var roomNo = await repository.FirstOrDefaultAsync(x => x.Id == id);
                if (roomNo == null)
                {
                    return ApiResult.Fail("房号不存在");
                }

                roomNo.RoomStatus = !roomNo.RoomStatus;
                await repository.UpdateAsync(roomNo);

                return ApiResult.Ok();
            }
            catch (Exception ex)
            {
                return ApiResult.Fail(msg: ex.Message);
            }
        }

     
    }
}
